package com.aiyige.base.api.httpinterceptor;

import android.text.TextUtils;

import com.aiyige.BuildConfig;
import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.location.LocationUtil;
import com.aiyige.location.model.RegionData;
import com.aiyige.model.DeviceInfo;
import com.aiyige.page.selectinterest.util.InterestUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.vondear.rxtools.RxDeviceTool;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xiachao on 2017/8/10.
 */

public class ApiHeaderInterceptor implements Interceptor {
    private static final String TAG = ApiHeaderInterceptor.class.getSimpleName();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        addHeaders(builder);
        Request request = builder.build();
        try {
            return chain.proceed(request);
        } catch (IOException e) {
            throw new IOException(StringUtils.getString(R.string.request_failed));
        }
    }

    private void addHeaders(Request.Builder builder) {
        List<String> industryIdList = InterestUtil.getIndustryId();
        RegionData regionData = LocationUtil.getCurrentUserSelectRegion();
        builder.addHeader("token", TextUtils.isEmpty(AccountUtil.getCurrentUser().getAccessToken().getAccess_token()) ? "" : "Bearer " + AccountUtil.getCurrentUser().getAccessToken().getAccess_token())
                .addHeader("deviceInfo", getAiyigeDeviceInfo())
                .addHeader("Eig_AppVersion", BuildConfig.appVersion)
                .addHeader("Eig_Application", "Aiyige_App")
                .addHeader("channel", ListUtil.isEmpty(industryIdList) ? "all" : TextUtils.join(",", industryIdList))
                .addHeader("city", regionData == null ? "" : regionData.getId());
    }

    private String getAiyigeDeviceInfo() {
//        1. signAccesstoken
//        2. signDeviceBrand 设备厂商品牌 samsung 小米 iPhone
//        3. signDeviceType  设备类型 丽日 Note3 或 6s
//        4. signDeviceCode 设备唯一码
//        5. signLanguage 语言
//        6. signOs android 或 ios
//        7. signOsVersion 操作系统版本
//        8. signAppVersion app的版本号
//        9. signSignature 这个是加密算法获得的
        String osVersion = android.os.Build.VERSION.RELEASE;//手机当前系统
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setSignAccesstoken(AccountUtil.getCurrentUser().getAccessToken().getAccess_token());
        deviceInfo.setSignDeviceBrand(RxDeviceTool.getBuildBrand());
        deviceInfo.setSignDeviceType(RxDeviceTool.getBuildMANUFACTURER());
        deviceInfo.setSignDeviceCode(RxDeviceTool.getSerialNumber());
        String language = MyApp.getInstance().getResources().getConfiguration().locale.getCountry();
        deviceInfo.setSignLanguage(language);
        deviceInfo.setSignOs("android");
        deviceInfo.setSignOsVersion(osVersion);
        deviceInfo.setSignAppVersion(BuildConfig.appVersion);
        deviceInfo.setSignAppId("wudao");
        deviceInfo.setSignSignature(deviceInfo.getSignature());
        String deviceInfoJson = JSON.toJSONString(deviceInfo);
        try {
            deviceInfoJson = URLEncoder.encode(deviceInfoJson, "utf-8");
        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
        }
        return deviceInfoJson;
    }


}
